﻿using System;

namespace Pfz.WpfControls
{
	/// <summary>
	/// Class responsible for setting the Editing function to the actual thread only.
	/// When disposed, it restores the last one.
	/// </summary>
	public sealed class EditorForThisThread:
		IDisposable
	{
		private Func<EditorArgs, object> _oldThreadEditing;

		/// <summary>
		/// Create a new instance of this class to set a new editing function to the actual thread.
		/// If you want to set it globally, set the static Editor.Editing property.
		/// </summary>
		public EditorForThisThread(Func<EditorArgs, object> editing)
		{
			_oldThreadEditing = Editor._threadEditing;
			Editor._threadEditing = editing;
		}

		/// <summary>
		/// Recovers the last Editing function for this thread.
		/// </summary>
		public void Dispose()
		{
			Editor._threadEditing = _oldThreadEditing;
		}
	}
}
